package com.spring.demo.tx;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @version 1.0.0
 * @Date: 2022/5/31 9:38
 * @Author ZhuYouBin
 * @Description
 */
public class MoneyService3 {

    private JdbcTemplate jdbcTemplate;
    public MoneyService3(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void transferMoney() {
        // A用户减少1000元
        String sql = "UPDATE spring.t_money SET balance = balance - 1000  WHERE id = 10001";
        int update = jdbcTemplate.update(sql);
        System.out.println("A用户减少1000元成功: " + update);

        // 模拟转账过程中出现异常
//        int i = 10 / 0;

        // B用户增加1000元
        sql = "UPDATE spring.t_money SET balance = balance + 1000  WHERE id = 10002";
        update = jdbcTemplate.update(sql);
        System.out.println("B用户减少1000元成功: " + update);
    }
}
